﻿#include "dialog.h"
#include "ui_dialog.h"
#include <QDebug>
#include <QCryptographicHash>
#include <QSqlQuery>
#include <QMessageBox>
#include "ZKFingerReader/PictureBox.h"
#include "EnrollFingerprintDialog.h"
#include "VerifyDialog.h"
#include "IdentifyDialog.h"
Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    initDB();

    connect(ui->pushButtonEnroll, SIGNAL(clicked(bool)), this, SLOT(showEnrollDialog()));
    connect(ui->pushButtonVerify, SIGNAL(clicked(bool)), this, SLOT(showVerifyDialog()));
    connect(ui->pushButtonIdentify, SIGNAL(clicked(bool)), this, SLOT(showIdentifyDialog()));
    connect(ui->pushButtonDelete, SIGNAL(clicked(bool)), this, SLOT(deleteUser()));
}
void Dialog::initDB()
{
    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(2);

    QStringList list;
    list << QString::fromUtf8(u8"工号");
    list << QString::fromUtf8(u8"姓名");
    ui->tableWidget->setHorizontalHeaderLabels(list);

    QMap<int, QString> map = m_dbLoader.userNames();
    QMapIterator<int, QString> iter(map);
    ui->tableWidget->setRowCount(map.size() + 1);

    int i = 0;
    while (iter.hasNext())
    {
        iter.next();
        int fid = iter.key();
        QString name = iter.value();
        QTableWidgetItem *itemFid = new QTableWidgetItem(tr("%1").arg(fid));
        QTableWidgetItem *itemName = new QTableWidgetItem(tr("%1").arg(name));
        ui->tableWidget->setItem(i, 0, itemFid);
        ui->tableWidget->setItem(i, 1, itemName);
        i++;
    }
}

QString md5(QString pwd)
{
    QCryptographicHash md5(QCryptographicHash::Md5);
    md5.addData(pwd.toLocal8Bit());
    QString ret(md5.result());
    return ret;
}

void Dialog::showEnrollDialog()
{
    EnrollFingerprintDialog enrollDialog;

    if( enrollDialog.exec() )
    {
        QString password = enrollDialog.password();
        QString name = enrollDialog.name();
        int userID = enrollDialog.user_id();
        QByteArray fp = enrollDialog.fpTemplate();
        m_dbLoader.addUser(userID, name, md5(password), fp);
    }
    initDB();
}

void Dialog::deleteUser()
{
    int row = ui->tableWidget->currentRow();
    QString fid = ui->tableWidget->item(row, 0)->text();
    if(fid == "")
    {

    }
    else
    {
        int userID = fid.toInt();
        QMessageBox box;
        box.setText(QString::fromUtf8(u8"即将删除工号 %1，请确认！").arg(fid));
        box.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
        box.setDefaultButton(QMessageBox::Cancel);
        if( box.exec() == QMessageBox::Ok)
        {
            m_dbLoader.deleteUser(userID);
        }
    }
    initDB();
}

void Dialog::showIdentifyDialog()
{
    IdentifyDialog dia;
    dia.exec();
}

void Dialog::showVerifyDialog()
{
    VerifyDialog dia;
    dia.exec();
}


Dialog::~Dialog()
{
    delete ui;
}
